package com.qf.smartplatform.event.listener;

import com.qf.smartplatform.pojo.SysHumiture;
import com.sun.media.jfxmediaimpl.HostUtils;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/*** @author Tgb
 * @version V1.0
 * @Project smartplatform
 * @Package com.qf.smartplatform.event.listener
 * @Description:
 * @Date 2022/9/16 11:20
 */
@Component
public class HumitureListener {
    private List<SysHumiture> humitureList=new ArrayList<>();
    @EventListener
    @Async
    public void onHumiture(SysHumiture humiture){
        humitureList.add(humiture);
    }
    @Scheduled(fixedRate = 30000)
    public void avgTemper(){
        if (humitureList.size()==0){
            return;
        }
        List<SysHumiture> tempers = new ArrayList<>();
        tempers.addAll(humitureList);
        humitureList.clear();
        double avgTemper = tempers.stream().mapToDouble(SysHumiture::getTemperature).average().getAsDouble();
        if (avgTemper>35){
            System.out.println("天太热啦，开空调~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        }else {
            System.out.println("天不热，忍着点：：：：：：：：：：：：：：：：：：：：：：：");
        }
    }
}
